/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package top.dzygod.modules.pay.service.impl;

import top.dzygod.modules.pay.domain.Grade;
import top.dzygod.utils.ValidationUtil;
import top.dzygod.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import top.dzygod.modules.pay.repository.GradeRepository;
import top.dzygod.modules.pay.service.GradeService;
import top.dzygod.modules.pay.service.dto.GradeDto;
import top.dzygod.modules.pay.service.dto.GradeQueryCriteria;
import top.dzygod.modules.pay.service.mapstruct.GradeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import top.dzygod.utils.PageUtil;
import top.dzygod.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://dzygod.top
* @description 服务实现
* @author dzygod
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class GradeServiceImpl implements GradeService {

    private final GradeRepository gradeRepository;
    private final GradeMapper gradeMapper;

    @Override
    public Map<String,Object> queryAll(GradeQueryCriteria criteria, Pageable pageable){
        Page<Grade> page = gradeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(gradeMapper::toDto));
    }

    @Override
    public List<GradeDto> queryAll(GradeQueryCriteria criteria){
        return gradeMapper.toDto(gradeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GradeDto findById(Long gradeId) {
        Grade grade = gradeRepository.findById(gradeId).orElseGet(Grade::new);
        ValidationUtil.isNull(grade.getId(),"Grade","gradeId",gradeId);
        return gradeMapper.toDto(grade);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GradeDto create(Grade resources) {
        return gradeMapper.toDto(gradeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Grade resources) {
        Grade grade = gradeRepository.findById(resources.getId()).orElseGet(Grade::new);
        ValidationUtil.isNull( grade.getId(),"Grade","id",resources.getId());
        grade.copy(resources);
        gradeRepository.save(grade);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long gradeId : ids) {
            gradeRepository.deleteById(gradeId);
        }
    }

    @Override
    public void download(List<GradeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GradeDto grade : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("年级名称", grade.getName());
            map.put("创建时间", grade.getGmtCreate());
            map.put("修改时间", grade.getGmtModified());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
